package com.ipzoe.common.retrofit.service;

import com.google.common.collect.Lists;
import com.ipzoe.common.retrofit.request.LeanCloudKick;
import com.ipzoe.common.retrofit.request.LeanCloudPushMessage;
import com.ipzoe.common.retrofit.response.BaseSuccess;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import retrofit2.Response;

import java.io.IOException;
import java.util.List;

/**
 * 消息推送Service
 * <p>
 * Created 2018/01/19.
 *
 * @author pfzhang
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
@CommonsLog
public class NewsPushService {

    /**
     * 频道前缀 用户
     */
    public static final String CHANNEL_PREFIX_USER = "common_user_";

    /**
     * 频道前缀 系统
     */
    public static final String CHANNEL_PREFIX_SYSTEM = "common_system";

    @Autowired
    private LeanCloudService leanCloudService;

    @Value("${leancloud.masterKey}")
    private String leanCloudMasterKey;

    /**
     * 个人消息推送
     *
     * @param accountId 用户ID
     * @param text      推送内容
     * @param key       自定义内容
     * @param type      自定义类型
     * @param slient    是否透传
     * @return 响应body内容
     * @throws IOException
     */
    public String push(Long accountId, String text, String key, String type, Boolean slient) throws IOException {

        Response<BaseSuccess> response =
                leanCloudService.push(leanCloudMasterKey,
                        new LeanCloudPushMessage(new String[]{CHANNEL_PREFIX_USER + accountId}, text, key, type, slient)).execute();
        if (!response.isSuccessful()) {
            return response.errorBody().string();
        }

        return response.body().toString();
    }

    /**
     * 个人消息推送
     *
     * @param accountId 用户ID
     * @param text      推送内容
     * @param key       自定义内容
     * @param type      自定义类型
     * @return 响应body内容
     * @throws IOException
     */
    public String push(Long accountId, String text, String key, String type) throws IOException {

        Response<BaseSuccess> response =
                leanCloudService.push(leanCloudMasterKey,
                        new LeanCloudPushMessage(new String[]{CHANNEL_PREFIX_USER + accountId}, text, key, type, null)).execute();
        if (!response.isSuccessful()) {
            return response.errorBody().string();
        }

        return response.body().toString();
    }

    /**
     * 群发消息推送
     *
     * @param accountIdList 群发用户ID集合
     * @param text          推送内容
     * @param key           自定义内容
     * @param type          自定义类型
     * @param slient        是否透传
     * @return 响应body内容
     * @throws IOException
     */
    public String pushList(List<Long> accountIdList, String text, String key, String type, Boolean slient) throws IOException {
        List<String> list = Lists.newArrayList();
        for (Long accountId : accountIdList) {
            list.add(CHANNEL_PREFIX_USER + accountId);
        }
        Response<BaseSuccess> response =
                leanCloudService.push(leanCloudMasterKey,
                        new LeanCloudPushMessage(list.toArray(new String[list.size()]), text, key, type, slient)).execute();
        if (!response.isSuccessful()) {
            return response.errorBody().string();
        }

        return response.body().toString();
    }

    /**
     * 群发消息推送
     *
     * @param accountIdList 群发用户ID集合
     * @param text          推送内容
     * @param key           自定义内容
     * @param type          自定义类型
     * @return 响应body内容
     * @throws IOException
     */
    public String pushList(List<Long> accountIdList, String text, String key, String type) throws IOException {
        List<String> list = Lists.newArrayList();
        for (Long accountId : accountIdList) {
            list.add(CHANNEL_PREFIX_USER + accountId);
        }
        Response<BaseSuccess> response =
                leanCloudService.push(leanCloudMasterKey,
                        new LeanCloudPushMessage(list.toArray(new String[list.size()]), text, key, type, null)).execute();
        if (!response.isSuccessful()) {
            return response.errorBody().string();
        }

        return response.body().toString();
    }

    /**
     * 给全部用户的通知
     *
     * @param text 推送内容
     * @return 响应body内容
     * @throws IOException
     */
    public String pushAll(String text) throws IOException {

        Response<BaseSuccess> response =
                leanCloudService.push(leanCloudMasterKey,
                        new LeanCloudPushMessage(new String[]{CHANNEL_PREFIX_SYSTEM}, text, LeanCloudPushMessage.TYPE_SYSTEM)).execute();
        if (!response.isSuccessful()) {
            return response.errorBody().string();
        }

        return response.body().toString();
    }

    /**
     * 用户，强制下线
     *
     * @param accountId 用户ID
     * @param text      强制下线理由
     */
    @Async
    public void kick(Long accountId, String text) {
        try {
            leanCloudService.kick(leanCloudMasterKey, new LeanCloudKick(CHANNEL_PREFIX_USER + accountId, text)).execute();
        } catch (IOException e) {
            log.error("推送强制下线失败,用户ID:" + accountId);
        }
    }
}
